package com.supermap.panes;

import com.supermap.Helper;
import com.supermap.MainFrame;
import com.supermap.analyst.spatialanalyst.BufferAnalyst;
import com.supermap.analyst.spatialanalyst.BufferAnalystParameter;
import com.supermap.analyst.spatialanalyst.BufferEndType;
import com.supermap.analyst.spatialanalyst.BufferRadiusUnit;
import com.supermap.data.*;
import com.supermap.data.Enum;
import com.supermap.dialogs.ProcessBarDialog;
import com.supermap.panes.advance.SettingPane;

import javax.swing.*;
import java.awt.*;

public class BufferAnalystSettingPane extends SettingPane {
    private JPanel mainPane;
    private JCheckBox leftBufferCbx;
    private JCheckBox rightBufferCbx;
    private JTextField lengthTxt;
    private JTextField leftRadiusTxt;
    private JTextField rightRadiusTxt;
    private JComboBox<String> datasourceCmb1;
    private JComboBox<String> datasetCmb1;
    private JComboBox<String> resultDatasourceCmb1;
    private JComboBox<BufferRadiusUnit> bufferRadiusUnitJCmb1;
    private JCheckBox unionBufferCbx1;
    private JCheckBox saveSrcFieldsCbx1;
    private JTextField semicircleLineSegmentTxt1;
    private JTextField resultDatasetTxt1;
    private JComboBox<String> datasourceCmb2;
    private JComboBox<String> datasetCmb2;
    private JComboBox<String> resultDatasourceCmb2;
    private JComboBox<BufferRadiusUnit> bufferRadiusUnitJCmb2;
    private JCheckBox unionBufferCbx2;
    private JCheckBox saveSrcFieldsCbx2;
    private JTextField semicircleLineSegmentTxt2;
    private JTextField resultDatasetTxt2;

    private Workspace workspace;
    private boolean isMultiMode;
    private boolean isLineData;
    private BufferEndType bufferEndType;
    private SteppedListener steppedListener;
    private ProcessBarDialog generateBufferProcessDialog;

    public BufferAnalystSettingPane(Workspace workspace, boolean isMultiMode) {
        super("缓冲区分析", new Object[]{workspace, isMultiMode});
    }

    @Override
    protected void initializeArgs(Object[] args) {
        this.workspace = (Workspace) args[0];
        this.isMultiMode = (Boolean) args[1];
        steppedListener = steppedEvent -> {
            int percent = steppedEvent.getPercent();
            generateBufferProcessDialog.setValue(percent);
            if (percent == 100) {
                generateBufferProcessDialog.setVisible(false);
                JOptionPane.showMessageDialog(getRootPane().getParent(), "缓冲区数据生成完成！", "生成缓冲区数据", JOptionPane.INFORMATION_MESSAGE);
            }
        };
        BufferAnalyst.addSteppedListener(steppedListener);
    }

    @Override
    protected void initializeCore() {
        super.confirmBtn.setText("分析");
        super.mainPanel.setLayout(new BorderLayout());

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout(FlowLayout.LEADING));

        JLabel dataTypeLbl = new JLabel("数据类型: ");
        topPanel.add(dataTypeLbl);

        JRadioButton pointOrPolygonRBtn = new JRadioButton("点、面数据");
        pointOrPolygonRBtn.addItemListener(e -> {
            JRadioButton radioBtn = (JRadioButton) e.getItem();
            if (radioBtn.isSelected()) {
                isLineData = false;
                if (mainPane != null) {
                    CardLayout cardLayout = (CardLayout) mainPane.getLayout();
                    cardLayout.previous(mainPane);
                }
            }
        });
        topPanel.add(pointOrPolygonRBtn);

        JRadioButton lineRBtn = new JRadioButton("线数据");
        lineRBtn.addItemListener(e -> {
            JRadioButton radioBtn = (JRadioButton) e.getItem();
            if (radioBtn.isSelected()) {
                isLineData = true;
                CardLayout cardLayout = (CardLayout) mainPane.getLayout();
                cardLayout.next(mainPane);
            }
        });
        topPanel.add(lineRBtn);

        // 添加单选约束
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(pointOrPolygonRBtn);
        buttonGroup.add(lineRBtn);
        pointOrPolygonRBtn.setSelected(true);

        super.mainPanel.add(topPanel, BorderLayout.NORTH);

        mainPane = new JPanel();
        CardLayout cardLayout = new CardLayout();
        mainPane.setLayout(cardLayout);
        datasourceCmb1 = new JComboBox<>();
        datasetCmb1 = new JComboBox<>();
        bufferRadiusUnitJCmb1 = new JComboBox<>();
        unionBufferCbx1 = new JCheckBox("合并缓冲区");
        saveSrcFieldsCbx1 = new JCheckBox("保留原对象字段属性");
        semicircleLineSegmentTxt1 = new JTextField();
        resultDatasourceCmb1 = new JComboBox<>();
        resultDatasetTxt1 = new JTextField();
        JPanel centerPanel1 = initCenterPanel(isLineData, datasourceCmb1, datasetCmb1, bufferRadiusUnitJCmb1, unionBufferCbx1,
                saveSrcFieldsCbx1, semicircleLineSegmentTxt1, resultDatasourceCmb1, resultDatasetTxt1);
        datasourceCmb2 = new JComboBox<>();
        datasetCmb2 = new JComboBox<>();
        bufferRadiusUnitJCmb2 = new JComboBox<>();
        unionBufferCbx2 = new JCheckBox("合并缓冲区");
        saveSrcFieldsCbx2 = new JCheckBox("保留原对象字段属性");
        semicircleLineSegmentTxt2 = new JTextField();
        resultDatasourceCmb2 = new JComboBox<>();
        resultDatasetTxt2 = new JTextField();
        JPanel centerPanel2 = initCenterPanel(!isLineData, datasourceCmb2, datasetCmb2, bufferRadiusUnitJCmb2, unionBufferCbx2,
                saveSrcFieldsCbx2, semicircleLineSegmentTxt2, resultDatasourceCmb2, resultDatasetTxt2);
        mainPane.add(centerPanel1);
        mainPane.add(centerPanel2);

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new FlowLayout());
        centerPanel.add(mainPane);

        super.mainPanel.add(centerPanel, BorderLayout.CENTER);
    }

    @Override
    protected void doConfirm() {
        JComboBox<String> datasourceCmb = isLineData ? datasourceCmb2 : datasourceCmb1;
        JComboBox<String> resultDatasourceCmb = isLineData ? resultDatasourceCmb2 : resultDatasourceCmb1;
        JComboBox<String> datasetCmb = isLineData ? datasetCmb2 : datasetCmb1;
        JTextField resultDatasetTxt = isLineData ? resultDatasetTxt2 : resultDatasetTxt1;
        JComboBox<BufferRadiusUnit> bufferRadiusUnitJCmb = isLineData ? bufferRadiusUnitJCmb2 : bufferRadiusUnitJCmb1;
        JTextField semicircleLineSegmentTxt = isLineData ? semicircleLineSegmentTxt2 : semicircleLineSegmentTxt1;
        JCheckBox unionBufferCbx = isLineData ? unionBufferCbx2 : unionBufferCbx1;
        JCheckBox saveSrcFieldsCbx = isLineData ? saveSrcFieldsCbx2 : saveSrcFieldsCbx1;

        if (datasourceCmb.getItemCount() == 0 || resultDatasourceCmb.getItemCount() == 0 || datasetCmb.getItemCount() == 0) {
            return;
        }

        Datasources datasources = workspace.getDatasources();
        String selectedDatasourceName = (String) datasourceCmb.getSelectedItem();
        Datasource datasource = datasources.get(selectedDatasourceName);
        if (datasource == null) {
            return;
        }

        String selectedDatasetName = (String) datasetCmb.getSelectedItem();
        DatasetVector srcDatasetVector = (DatasetVector) datasource.getDatasets().get(selectedDatasetName);

        Datasource resultDatasource = datasources.get((String) resultDatasourceCmb.getSelectedItem());
        if (resultDatasource == null) {
            return;
        }

        DatasetVectorInfo datasetVectorInfoBuffer = new DatasetVectorInfo();
        datasetVectorInfoBuffer.setType(DatasetType.REGION);
        datasetVectorInfoBuffer.setName(resultDatasetTxt.getText());
        datasetVectorInfoBuffer.setEncodeType(EncodeType.NONE);
        DatasetVector resultDatasetVector = resultDatasource.getDatasets().create(datasetVectorInfoBuffer);

        BufferAnalystParameter bufferAnalystParameter = new BufferAnalystParameter();
        bufferAnalystParameter.setEndType(bufferEndType);
        bufferAnalystParameter.setLeftDistance(Double.parseDouble(leftRadiusTxt.getText()));
        bufferAnalystParameter.setRightDistance(Double.parseDouble(rightRadiusTxt.getText()));
        bufferAnalystParameter.setRadiusUnit((BufferRadiusUnit) bufferRadiusUnitJCmb.getSelectedItem());
        bufferAnalystParameter.setSemicircleLineSegment(Integer.parseInt(semicircleLineSegmentTxt.getText()));

        if (generateBufferProcessDialog == null) {
            generateBufferProcessDialog = new ProcessBarDialog((Frame) getRootPane().getParent(), "生成缓冲区数据", true);
        }

        new Thread(() -> {
            if (!isMultiMode) {
                if (BufferAnalyst.createBuffer(srcDatasetVector, resultDatasetVector, bufferAnalystParameter,
                        unionBufferCbx.isSelected(), saveSrcFieldsCbx.isSelected())) {
                    Helper.showDatasetVectorsComparison("缓冲区分析结果", srcDatasetVector, resultDatasetVector, (MainFrame) getRootPane().getParent());
                } else {
                    JOptionPane.showMessageDialog(getRootPane().getParent(), "缓冲区分析失败!", "缓冲区分析", JOptionPane.ERROR_MESSAGE);
                }
            } else {
                if (BufferAnalyst.createMultiBuffer(srcDatasetVector, resultDatasetVector, new double[]{10, 20, 30},
                        (BufferRadiusUnit) bufferRadiusUnitJCmb.getSelectedItem(), Integer.parseInt(semicircleLineSegmentTxt.getText()),
                        unionBufferCbx.isSelected(), saveSrcFieldsCbx.isSelected(), false)) {
                    Helper.showDatasetVectorsComparison("多重缓冲区分析结果", srcDatasetVector, resultDatasetVector, (MainFrame) getRootPane().getParent());
                } else {
                    JOptionPane.showMessageDialog(getRootPane().getParent(), "多重缓冲区分析失败!", "缓冲区分析", JOptionPane.ERROR_MESSAGE);
                }
            }
        }).start();
        generateBufferProcessDialog.setVisible(true);
    }

    @Override
    protected void doClose() {
        BufferAnalyst.removeSteppedListener(steppedListener);
        if (generateBufferProcessDialog != null) {
            generateBufferProcessDialog.dispose();
        }
    }

    private JPanel initCenterPanel(boolean isLineData, JComboBox<String> datasourceCmb, JComboBox<String> datasetCmb,
                                   JComboBox<BufferRadiusUnit> bufferRadiusUnitJCmb, JCheckBox unionBufferCbx,
                                   JCheckBox saveSrcFieldsCbx, JTextField semicircleLineSegmentTxt,
                                   JComboBox<String> resultDatasourceCmb, JTextField resultDatasetTxt) {
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridBagLayout());

        JPanel bufferDataPanel = new JPanel();
        bufferDataPanel.setBorder(BorderFactory.createTitledBorder("缓冲数据"));
        bufferDataPanel.setLayout(new GridBagLayout());

        JLabel datasourceLbl = new JLabel("数据源: ");
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        bufferDataPanel.add(datasourceLbl, gbc);

        Datasources datasources = workspace.getDatasources();
        int datasourcesCount = datasources.getCount();
        if (datasourcesCount > 0) {
            for (int i = 0; i < datasourcesCount; i++) {
                Datasource datasource = datasources.get(i);
                datasourceCmb.addItem(datasource.getAlias());
            }
        }
        JComboBox<String> finalDatasetCmb = datasetCmb;
        datasourceCmb.addItemListener(e -> {
            String selectedDatasourceName = (String) e.getItem();
            Datasource datasource = datasources.get(selectedDatasourceName);
            loadDatasets(finalDatasetCmb, datasource);
        });
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 10;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        bufferDataPanel.add(datasourceCmb, gbc);

        JLabel datasetLbl = new JLabel("数据集: ");
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        bufferDataPanel.add(datasetLbl, gbc);

        if (datasourcesCount > 0) {
            Datasource datasource = datasources.get((String) datasourceCmb.getSelectedItem());
            loadDatasets(datasetCmb, datasource);
        }
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 10;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        bufferDataPanel.add(datasetCmb, gbc);

        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 2;
        gbc.insets = Helper.DefaultInsets;
        centerPanel.add(bufferDataPanel, gbc);

        JPanel bufferRadiusPanel = new JPanel();
        bufferRadiusPanel.setBorder(BorderFactory.createTitledBorder("缓冲半径"));
        bufferRadiusPanel.setLayout(new GridBagLayout());

        JLabel unitLbl = new JLabel("单位:   ");
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        bufferRadiusPanel.add(unitLbl, gbc);

        Enum[] bufferRadiusUnits = BufferRadiusUnit.getEnums(BufferRadiusUnit.class);
        for (int i = 0; i < bufferRadiusUnits.length; i++) {
            BufferRadiusUnit bufferRadiusUnit = (BufferRadiusUnit) bufferRadiusUnits[i];
            bufferRadiusUnitJCmb.addItem(bufferRadiusUnit);
        }
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 10;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        bufferRadiusPanel.add(bufferRadiusUnitJCmb, gbc);

        if (!isLineData) {
            JLabel lengthLbl = new JLabel("长度:   ");
            gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.insets = Helper.DefaultInsets;
            bufferRadiusPanel.add(lengthLbl, gbc);

            lengthTxt = new JTextField();
            lengthTxt.setText("12");
            gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 1;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.weightx = 10;
            gbc.weighty = 1;
            gbc.insets = Helper.DefaultInsets;
            bufferRadiusPanel.add(lengthTxt, gbc);
        } else {
            JLabel leftRadiusLbl = new JLabel("左半径: ");
            gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.insets = Helper.DefaultInsets;
            bufferRadiusPanel.add(leftRadiusLbl, gbc);

            leftRadiusTxt = new JTextField();
            leftRadiusTxt.setText("12");
            gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 1;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.weightx = 10;
            gbc.weighty = 1;
            gbc.insets = Helper.DefaultInsets;
            bufferRadiusPanel.add(leftRadiusTxt, gbc);

            JLabel rightRadiusLbl = new JLabel("右半径: ");
            gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy = 2;
            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.insets = Helper.DefaultInsets;
            bufferRadiusPanel.add(rightRadiusLbl, gbc);

            rightRadiusTxt = new JTextField();
            rightRadiusTxt.setText("12");
            gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 1;
            gbc.gridy = 2;
            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.weightx = 10;
            gbc.weighty = 1;
            gbc.insets = Helper.DefaultInsets;
            bufferRadiusPanel.add(rightRadiusTxt, gbc);

            JPanel bufferParametersPanel = new JPanel();
            bufferParametersPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

            JRadioButton roundBufferRBtn = new JRadioButton("圆头缓冲");
            roundBufferRBtn.addItemListener(e -> {
                JRadioButton bufferRBtn = (JRadioButton) e.getItem();
                if (bufferRBtn.isSelected()) {
                    bufferEndType = BufferEndType.ROUND;
                    leftBufferCbx.setEnabled(false);
                    rightBufferCbx.setEnabled(false);
                }
            });
            bufferParametersPanel.add(roundBufferRBtn);

            JRadioButton flatBufferRBtn = new JRadioButton("平头缓冲");
            flatBufferRBtn.addItemListener(e -> {
                JRadioButton bufferRBtn = (JRadioButton) e.getItem();
                if (bufferRBtn.isSelected()) {
                    bufferEndType = BufferEndType.FLAT;
                    leftBufferCbx.setEnabled(true);
                    rightBufferCbx.setEnabled(true);
                }
            });
            bufferParametersPanel.add(flatBufferRBtn);

            ButtonGroup buttonGroup = new ButtonGroup();
            buttonGroup.add(roundBufferRBtn);
            buttonGroup.add(flatBufferRBtn);

            leftBufferCbx = new JCheckBox("左缓冲");
            leftBufferCbx.setSelected(true);
            bufferParametersPanel.add(leftBufferCbx);

            rightBufferCbx = new JCheckBox("右缓冲");
            rightBufferCbx.setSelected(true);
            bufferParametersPanel.add(rightBufferCbx);

            gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.gridwidth = 2;
            gbc.gridheight = 1;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.insets = Helper.DefaultInsets;
            bufferRadiusPanel.add(bufferParametersPanel, gbc);

            roundBufferRBtn.setSelected(true);
        }

        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = isLineData ? 4 : 2;
        gbc.insets = Helper.DefaultInsets;
        centerPanel.add(bufferRadiusPanel, gbc);

        JPanel bufferParametersSettingPanel = new JPanel();
        bufferParametersSettingPanel.setBorder(BorderFactory.createTitledBorder("参数设置"));
        bufferParametersSettingPanel.setLayout(new GridBagLayout());

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        bufferParametersSettingPanel.add(unionBufferCbx, gbc);

        saveSrcFieldsCbx.setSelected(true);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        bufferParametersSettingPanel.add(saveSrcFieldsCbx, gbc);

        JLabel semicircleLineSegmentLab = new JLabel("半圆弧线段数(4-200): ");
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        bufferParametersSettingPanel.add(semicircleLineSegmentLab, gbc);

        semicircleLineSegmentTxt.setText("100");
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 10;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        bufferParametersSettingPanel.add(semicircleLineSegmentTxt, gbc);

        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 3;
        gbc.insets = Helper.DefaultInsets;
        centerPanel.add(bufferParametersSettingPanel, gbc);

        JPanel resultDataPanel = new JPanel();
        resultDataPanel.setBorder(BorderFactory.createTitledBorder("结果数据"));
        resultDataPanel.setLayout(new GridBagLayout());

        JLabel resultDatasourceLbl = new JLabel("数据源: ");
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        resultDataPanel.add(resultDatasourceLbl, gbc);

        if (datasourcesCount > 0) {
            for (int i = 0; i < datasourcesCount; i++) {
                Datasource datasource = datasources.get(i);
                resultDatasourceCmb.addItem(datasource.getAlias());
            }
        }
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 10;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        resultDataPanel.add(resultDatasourceCmb, gbc);

        JLabel resultDatasetLbl = new JLabel("数据集: ");
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        resultDataPanel.add(resultDatasetLbl, gbc);

        resultDatasetTxt.setText("result_buffer");
        if (datasourcesCount > 0) {
            Datasource datasource = datasources.get((String) resultDatasourceCmb.getSelectedItem());
            String availableDatasetName = datasource.getDatasets().getAvailableDatasetName(resultDatasetTxt.getText());
            resultDatasetTxt.setText(availableDatasetName);
        }
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 10;
        gbc.weighty = 1;
        gbc.insets = Helper.DefaultInsets;
        resultDataPanel.add(resultDatasetTxt, gbc);

        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1;
        gbc.weighty = 2;
        gbc.insets = Helper.DefaultInsets;
        centerPanel.add(resultDataPanel, gbc);
        return centerPanel;
    }

    private void loadDatasets(JComboBox<String> datasetCmb, Datasource datasource) {
        Datasets datasets = datasource.getDatasets();
        int datasetsCount = datasets.getCount();
        if (datasetsCount > 0) {
            for (int i = 0; i < datasetsCount; i++) {
                Dataset dataset = datasets.get(i);
                if (dataset instanceof DatasetVector) {
                    datasetCmb.addItem(dataset.getName());
                }
            }
        }
    }
}
